use [family-annal]
GO
-- Drop trigger in table tb_fa_born
IF OBJECT_ID ('tb_fa_born.trigger_fa_born_insert_update','TR') IS NOT NULL
   DROP TRIGGER tb_fa_born.trigger_fa_born_insert_update;
GO
-- Drop table and all constraint if exists
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tb_fa_born]') AND name = N'pk_tb_fa_born')
BEGIN
ALTER TABLE [dbo].[tb_fa_born] DROP CONSTRAINT [pk_tb_fa_born]
END
GO
USE [family-annal]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_fa_born]') AND type in (N'U'))
DROP TABLE [dbo].[tb_fa_born]
GO
use [family-annal]
GO
-- create table person
create table tb_fa_born
(
	born_id int identity(1,1) not null,
	born_info nvarchar(max),
	born_file_code varchar(max),
	create_date datetime,
	create_uid int,
	update_date datetime,
	update_uid int,
	born_status varchar(1) default 'Y'
);
-- create constraint primary key
alter table tb_fa_born
		add constraint pk_tb_fa_born primary key (born_id);
GO
use [family-annal];
GO
-- create trigger for create_date
CREATE TRIGGER trigger_fa_born_insert_update
   ON  tb_fa_born 
   AFTER INSERT, UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
    IF EXISTS(SELECT * FROM INSERTED)
    BEGIN
		update tb_fa_born set create_date = GETDATE()
		from tb_fa_born t join inserted i
		on t.born_id = i.born_id;
    END
    IF NOT EXISTS(SELECT * FROM UPDATED)
    BEGIN
		update tb_fa_born set update_date = GETDATE()
		from tb_fa_born t join updated u
		on t.born_id = u.born_id;
    END
END
GO